package algorithm.leetcode.simple;

/*
 计算并返回 x 的平方根，其中 x 是非负整数。
 */

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/9/30
 */
public class Question69 {

    public static void main(String[] args) {
        Question69 question = new Question69();
        int val = question.mySqrt(2112312412);
        System.out.println(val);
    }

    public int mySqrt(int x) {
        int left = 0;
        // 考虑 1
        int right = x / 2 + 1;
        while (left < right) {
            // 无符号右移
            int mid = (left + right + 1) >>> 1;
            int val = mid * mid;
            if (val > x) {
                right = mid - 1;
            } else {
                left = mid;
            }
        }
        return left;
    }
}
